package virtorina.server.domain;

import java.io.Serializable;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.Extension;

import virtorina.client.room.dto.UserDTO;
import virtorina.server.PMF;

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
public class User implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long UserID;
	@Persistent
	private String UserProfile;
	@Persistent
	private String name;
	@Persistent
	private Integer rank;
	/*
	 * -3 - banned -2 - muted -1 - anonymous 0 - just user 1 - helper 2 -
	 * moderator 3 - administrator
	 */
	@NotPersistent
	private int lastMessage;
	@NotPersistent
	private int lastUserCome;
	@NotPersistent
	private int lastUserLeave;
	@NotPersistent
	private long nextTime;
	@NotPersistent
	private boolean isComplaint;

	public User(UserDTO user) {
		this.UserID = user.getID();
		this.name = user.getName();
	}

	public User() {

	}

	public User(Long UserID, String name) {
		this.UserID = UserID;
		this.name = name;
	}

	public String getUserProfile() {
		return UserProfile;
	}

	public void setUserProfile(String userProfile) {
		UserProfile = userProfile;
	}

	public Long getUserID() {
		return UserID;
	}

	public void setUserID(Long userID) {
		UserID = userID;
	}

	public int getLastMessage() {
		return lastMessage;
	}

	public void setLastMessage(int lastMessage) {
		this.lastMessage = lastMessage;
	}

	public String getName() {
		if (name == null){
			name = "Anonymous";
		}
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public static int getRank(Long UserID) {
		if (UserID < 0) {
			return -1;
		}
		PersistenceManager pm = PMF.get().getPersistenceManager();
		User e = pm.getObjectById(User.class, UserID);
		if (e.getRank() == null) {
			if (UserID.equals(54003L) || UserID.equals(58003L)) {
				e.setRank(2);
			} else if (UserID.equals(45001L) || UserID.equals(25003L)) {
				e.setRank(3);
			} else {
				e.setRank(0);
			}
			pm.makePersistent(e);
		}
		pm.close();
		return e.getRank();
	}

	public static boolean isFreeName(String nick) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query q = pm.newQuery("select from " + User.class.getName()
				+ " where name == nick");
		q.declareParameters("String nick");
		List<User> rslt = (List<User>) q.execute(nick);
		boolean b;
		if (rslt.size() == 0) {
			b = true;
		} else {
			b = false;
		}
		pm.close();
		return b;
	}

	public static User getByProfile(String uProf) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query q = pm.newQuery("select from " + User.class.getName()
				+ " where UserProfile == uProf");
		q.declareParameters("String uProf");
		List<User> rslt = (List<User>) q.execute(uProf);
		User user;
		if (rslt.size() == 0) {
			user = null;
		} else {
			user = rslt.get(0);
		}
		pm.close();
		return user;
	}

	public static User getUser(Long UserID) {
		if (UserID == null || UserID < 0){
			return new User();
		}
		PersistenceManager pm = PMF.get().getPersistenceManager();
		User e = null;
		try {
			e = pm.getObjectById(User.class, UserID);
		} catch (Exception ex) {
			e = null;
		}
		pm.close();
		return e;
	}

	public static User addUser(String UserProfile) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		User user = new User();
		user.setUserProfile(UserProfile);
		try {
			return pm.makePersistent(user);
		} finally {
			pm.close();
		}
	}

	public static void updateUser(User user) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		User e = pm.getObjectById(User.class, user.getUserID());
		e.setName(user.getName());
		// TODO
		pm.close();
	}

	public int getLastUsersCome() {
		return lastUserCome;
	}

	public void setLastUsersCome(int LastUsersCome) {
		this.lastUserCome = LastUsersCome;
	}

	public int getLastUsersLeave() {
		return lastUserLeave;
	}

	public void setLastUsersLeave(int lastUserLeave) {
		this.lastUserLeave = lastUserLeave;
	}

	public long getNextTime() {
		return nextTime;
	}

	public void setNextTime(long nextTime) {
		this.nextTime = nextTime;
	}

	public boolean isComplaint() {
		return isComplaint;
	}

	public void setComplaint(boolean isComplaint) {
		this.isComplaint = isComplaint;
	}

	public Integer getRank() {
		return rank;
	}

	public void setRank(Integer rank) {
		this.rank = rank;
	}

}
